package com.chain.chl.ui.fullimage;

import android.app.SharedElementCallback;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.chain.chl.R;
import com.chain.chl.util.FrescoLoadUtils;
import com.chain.chl.util.Photo;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

public class FullScreenImageActivity extends AppCompatActivity implements DragToFinishPhotoView.OnPhotoViewDragListener {

	public static final String EXTRA_DEFAULT_INDEX = "index";
	public static final String EXTRA_EXIT_INDEX = "exit_index";

	private WeakHashMap<String, View> mTransitionNameToView = new WeakHashMap<>();

	@BindView(R.id.view_pager) HackyViewPager viewPager;
	@BindView(R.id.v_bg_mask) View v_bg_mask;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_full_screen_image);
		ButterKnife.bind(this);

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
		}

		final int defaultIndex = getIntent().getIntExtra(EXTRA_DEFAULT_INDEX, 0);

		supportPostponeEnterTransition();
		viewPager.setAdapter(new PagerAdapter() {
			@Override
			public int getCount() {
				return Photo.PHOTO_LIST.length;
			}

			@Override
			public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
				return view == object;
			}

			@NonNull
			@Override
			public Object instantiateItem(@NonNull ViewGroup container, final int position) {
				final DragToFinishPhotoView photoView = new DragToFinishPhotoView(getApplicationContext());
				container.addView(photoView);
				photoView.setOnPhotoViewDragListener(FullScreenImageActivity.this);
				if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
					photoView.setTransitionName(String.valueOf(position));
					mTransitionNameToView.put(String.valueOf(position), photoView);
				}
				Glide.with(FullScreenImageActivity.this).load(Photo.PHOTO_LIST[position])
					.listener(new RequestListener<String, GlideDrawable>() {
						@Override public boolean onException(Exception e, String model,
							Target<GlideDrawable> target, boolean isFirstResource) {
							return false;
						}

						@Override
						public boolean onResourceReady(GlideDrawable resource, String model,
							Target<GlideDrawable> target, boolean isFromMemoryCache,
							boolean isFirstResource) {
							if (position == defaultIndex) {
								photoView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
									@Override
									public boolean onPreDraw() {
										photoView.getViewTreeObserver().removeOnPreDrawListener(this);
										supportStartPostponedEnterTransition();
										return false;
									}
								});
							}
							return false;
						}
					}).into(photoView);
				return photoView;
			}

			@Override
			public void destroyItem(@NonNull ViewGroup container, int position, @NonNull
                Object object) {
				container.removeView((View) object);
				mTransitionNameToView.remove(String.valueOf(position));
			}
		});
		viewPager.setCurrentItem(defaultIndex);
	}

	@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
	@Override
	public void onDragToFinish() {
		finishAfterTransition();
	}

	@Override
	public void onDragOffset(float offset, float maxOffset) {
		v_bg_mask.setAlpha(1 - offset / maxOffset);
	}

	@Override
	public void finishAfterTransition() {
		Intent intent = new Intent();
		intent.putExtra(EXTRA_EXIT_INDEX, viewPager.getCurrentItem());
		setResult(RESULT_OK, intent);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			setEnterSharedElementCallback(new SharedElementCallback() {
				@Override
				public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
					names.clear();
					sharedElements.clear();
					names.add(String.valueOf(viewPager.getCurrentItem()));
					View view = mTransitionNameToView.get(String.valueOf(viewPager.getCurrentItem()));
					if (view != null) {
						sharedElements.put(String.valueOf(viewPager.getCurrentItem()), view);
					}
				}
			});
		}
		super.finishAfterTransition();
	}
}
